﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.Mtbl;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.DALFactory;
using System.Linq.Dynamic;
namespace HKEA.CMS.Core.DAL.Mtbl
{
    public class mtblIndustryDAL:ImtblIndustry
    {
        HKEA_CMSEntities model;
        mtblIndustry dustry;
        public mtblIndustryDAL()
        {
            model = DataAccess.GetModelInstance();
        }
        public static mtblIndustryDAL GetInstance()
        {
            return new mtblIndustryDAL();
        }
        public int InsertENT(mtblIndustry ent)
        {
            ent.CreationDate = DateTime.Now;
            ent.LastModifyDate = DateTime.Now;
            model.mtblIndustries.AddObject(ent);
            return model.SaveChanges();
        }

        public List<mtblIndustry> GetListByENT(mtblIndustry ent, PageParam param, string strPropertyName,string strOrderBy)
        {
            var result = model.mtblIndustries.Where(a => a.IndustryUID > 0);
            if (ent.IndustryUID > 0)
                result = result.Where(a => a.IndustryUID == ent.IndustryUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            param.Count = result.Count();
            result = result.OrderBy(strPropertyName + " " + strOrderBy).Skip((param.CurrentPage - 1) * param.PageSize).Take(param.PageSize);
            return result.ToList();
        }
        public List<mtblIndustry> GetListByENT(mtblIndustry ent)
        {
            var result = model.mtblIndustries.Where(a => a.IndustryUID > 0);
            if (ent.IndustryUID > 0)
                result = result.Where(a => a.IndustryUID == ent.IndustryUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.ToList();
        }
        public int GetCountByENT(mtblIndustry ent)
        {
            var result = model.mtblIndustries.Where(a => a.IndustryUID > 0);
            if (ent.IndustryUID > 0)
                result = result.Where(a => a.IndustryUID == ent.IndustryUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.Count();
        }

        public int UpdateENT(mtblIndustry ent)
        {
            dustry = model.mtblIndustries.Where(a=>a.IndustryUID==ent.IndustryUID).FirstOrDefault();
            if (dustry != null)
            {
                dustry.Deleted = ent.Deleted;
                dustry.Industry_Chi = ent.Industry_Chi;
                dustry.Industry_Eng = ent.Industry_Eng;
                dustry.LastModifyBy = ent.LastModifyBy;
                dustry.LastModifyDate = DateTime.Now;
            }
            return model.SaveChanges();
        }

        public int DeleteENT(mtblIndustry ent)
        {
            dustry = model.mtblIndustries.Where(a => a.IndustryUID == ent.IndustryUID).FirstOrDefault();
            if (dustry != null)
            {
                model.mtblIndustries.DeleteObject(dustry);
            }
            return model.SaveChanges();
        }
    }
}
